package yyds.sniarbtej;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

public final class FileVisitor extends SimpleFileVisitor<Path> {
    private final List<String> productNames;
    private final Path configPath;

    public FileVisitor(List<String> productNames, Path configPath) {
        super();
        this.productNames = productNames;
        this.configPath = configPath;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        if (!file.getFileName().toString().endsWith(".jar")) {
            return FileVisitResult.CONTINUE;
        } else {
            JarFile jarFile = new JarFile(file.toFile());
            ZipEntry pluginXml = jarFile.getEntry("META-INF/plugin.xml");
            if (pluginXml == null) {
                return FileVisitResult.CONTINUE;
            } else {
                try {
                    InputStream is = jarFile.getInputStream(pluginXml);

                    try {
                        InputStreamReader streamReader = new InputStreamReader(is);

                        try {
                            BufferedReader reader = new BufferedReader(streamReader);

                            String line;
                            try {
                                while ((line = reader.readLine()) != null) {
                                    if (line.contains("<product-descriptor")) {
                                        String code = line.split("code=\"")[1].split("\"")[0];
                                        System.out.println("Found plugin product code: " + code);
                                        if (this.productNames.contains(code)) {
                                            line = "plugin_" + code + ".license";
                                            LicenseManager.checkLicenseFile(this.configPath.resolve(line).toFile());
                                        }
                                        break;
                                    }
                                }
                            } catch (Throwable e) {
                                try {
                                    reader.close();
                                } catch (Throwable t) {
                                    e.addSuppressed(t);
                                }

                                throw e;
                            }

                            reader.close();
                        } catch (Throwable e) {
                            try {
                                streamReader.close();
                            } catch (Throwable t) {
                                e.addSuppressed(t);
                            }

                            throw e;
                        }

                        streamReader.close();
                    } catch (Throwable e) {
                        if (is != null) {
                            try {
                                is.close();
                            } catch (Throwable t) {
                                e.addSuppressed(t);
                            }
                        }

                        throw e;
                    }

                    is.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return FileVisitResult.CONTINUE;
            }
        }
    }

}
